<!-- 个人资料页面 -->
<template>
  <div class="profile-page">
    <div class="user-info">
      <div class="container">
        <div class="row">
          <div class="col-xs-12 col-md-10 offset-md-1">
            <img :src="articleAuthor.image" class="user-img" />
            <h4>{{ articleAuthor.username }}</h4>
            <p>{{ articleAuthor.bio }}</p>
            <nuxt-link
              class="btn btn-sm btn-outline-secondary action-btn"
              :to="{
                name: 'settings',
              }"
            >
              <i class="ion-gear-a"></i>
              Edit Profile Settings
            </nuxt-link>
          </div>
        </div>
      </div>
    </div>

    <div class="container">
      <div class="row">
        <div class="col-xs-12 col-md-10 offset-md-1">
          <div class="articles-toggle">
            <ul class="nav nav-pills outline-active">
              <li class="nav-item">
                <nuxt-link
                  class="nav-link"
                  :to="{
                    path: `/profile/${articleAuthor.username}`,
                  }"
                  exact
                  >My Articles</nuxt-link
                >
              </li>
              <li class="nav-item">
                <nuxt-link
                  class="nav-link"
                  :to="{
                    path: `/profile/${articleAuthor.username}`,
                    query: {
                      favorites: 'favorites',
                    },
                  }"
                  exact
                  :class="{
                    active: favorites === 'favorites',
                  }"
                  >Favorited Articles</nuxt-link
                >
              </li>
            </ul>
          </div>

          <article-intro
            v-for="article in articles"
            :article="article"
            :key="article.slug"
            @favorite="onFavorite(article)"
            :btnDisabled="article.favoriteDisabled"
          />

          <nav>
            <ul class="pagination">
              <li
                class="page-item"
                :class="{ active: page === item }"
                v-for="item in totalPage"
                :key="item"
              >
                <nuxt-link
                  :to="{
                    name: 'profile',
                    query: {
                      page: item,
                      favorites: favorites,
                    },
                  }"
                  class="page-link"
                  >{{ item }}</nuxt-link
                >
              </li>
            </ul>
          </nav>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getArticles } from "@/api/article";
import ArticleIntro from "@/pages/profile/components/article-intro";
import favoriteManipulate from "@/utils/onFavorite";

export default {
  middleware: ["authenticated"],
  name: "Profile",
  async asyncData({ query, params: routeParams }) {
    const page = Number.parseInt(query.page) || 1;
    const limit = 3;
    const username = routeParams.username;
    const params = {
      limit,
      offset: (page - 1) * limit,
    };
    if (query.favorites === "favorites") {
      params.favorited = username;
    } else {
      params.author = username;
    }
    const { data } = await getArticles(params);
    const articles = data.articles;
    const articlesCount = data.articlesCount;
    articles.forEach((article) => (article.favoriteDisabled = false));
    const result = {
      articles,
      articlesCount,
      page,
      limit,
      favorites: undefined,
    };
    if (query.favorites === "favorites") {
      result.favorites = "favorites";
    }
    return result;
  },
  watchQuery: ["favorites", "page"],
  components: {
    ArticleIntro,
  },

  computed: {
    ...mapState(["user", "articleAuthor"]),
    totalPage() {
      return Math.ceil(this.articlesCount / this.limit);
    },
  },
  methods: {
    onFavorite(article) {
      favoriteManipulate(article);
    },
  },
};
</script>
